package com.atguigu.gulimall.cart.vo;

import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * 购物车视图
 *
 * @author DuGuanghan
 * @since 2025-02-13 16:16:19
 */
public class CartVo {
    /**
     * 商品详情
     */
    private List<CartItemVo> items;
    /**
     * 商品数量
     */
    private Integer countNum;
    /**
     * 商品类型数量
     */
    private Integer countType;
    /**
     * 商品总价格
     */
    private BigDecimal totalAmount;
    /**
     * 优惠金额
     */
    private BigDecimal reduce = new BigDecimal("0");

    public List<CartItemVo> getItems() {
        return items;
    }

    public void setItems(List<CartItemVo> items) {
        this.items = items;
    }

    public Integer getCountNum() {
        int num = 0;
        if (!CollectionUtils.isEmpty(items)) {
            for (CartItemVo item : items) {
                num += item.getCount();
            }
        }
        return num;
    }

    public void setCountNum(Integer countNum) {
        this.countNum = countNum;
    }

    public Integer getCountType() {
        return items.size();
    }

    public void setCountType(Integer countType) {
        this.countType = countType;
    }

    public BigDecimal getTotalAmount() {
        BigDecimal total = new BigDecimal("0");
        if (!CollectionUtils.isEmpty(items)) {
            for (CartItemVo item : items) {
                if (item.getCheck()) {
                    BigDecimal itemTotal = item.getTotalPrice();
                    total = total.add(itemTotal);
                }
            }
        }

        // 减去优惠
        total = total.subtract(getReduce());

        return total;
    }

    public void setTotalAmount(BigDecimal totalAmount) {
        this.totalAmount = totalAmount;
    }

    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }
}
